import util from "../util";
export default class parentModel {
    constructor(clz,api) {
        this.util = util;
        this.record = {};
        this.info = null;
        this.pointer = null;
        this.modelMap=null;
        this.api=api;
        this.clz=clz;
        /** modelMap格式
         * {
            table: tableName,
            featureList: new Set(),
            valueList: new Set(),
            fieldList: new Set(),
            groupList: new Set(),
            fieldMap: {
            fieldname；fieldtype
            },
          }
         */
    }
    setRecord(record){
        this.record=record;
        this._setPointer();
    }
    _setPointer(empty=true){
        if(!empty){
            this.info=null;
            this.pointer=null;
            this.modelMap=null;
            return;
        }
        this.pointer={
            id:this.record.id,
            type:this.record.type,
            className:this.record.className,
        };
        this.info={
            id:this.record.id,
            name:this.record.name,
            type:this.record.type,
            className:this.record.className,
            version:this.record.version,
            published:this.record.published,
        };
        // this.modelMap=this._getModelMap();
    }
    _getModelMap(){
        return util.modelState.map.get([this.pointer.type,this.pointer.className].join(','));
    }
    /*******************************************************************************
     * 对象属性编辑功能
     * ******************************************************************************/
    /***
     * 新增
     * @param record
     * @returns {Promise<boolean|*>}
     */
    async add(record){
        if(!record||!record.type||!record.className||record.type==''||record.className==''){
            return false;
        }
        let newRecord=Object.assign(this.clz.tpl(),record);
        let result = await this.api.add(newRecord).then(data=>{
            if(!!data){
                this.setRecord(data);
                console.log(data)
                return true;
            }else{
                return false;
            }
        }).catch(err=>{
            return false;
        });
        return result;
    }

    /**
     * 删除
     * @returns {Promise<boolean|*>}
     */
    async deleteById(id=this.record.id){
        let result = await this.api.deleteById(id).then(flag=>{
            if(flag){
                this.setRecord([]);
                return true;
            }else{
                return false;
            }
        }).catch(err=>{
            return false;
        });
        return result;

    }
    async delete(filter){
        let result = await this.api.delete({condition:filter}).then(flag=>{
            if(flag){
                this.setRecord([]);
                return true;
            }else{
                return false;
            }
        }).catch(err=>{
            return false;
        });
        return result;
    }

    /**
     * 更新
     * @returns {Promise<*>}
     */
    async update(data=this.record){
        /*let updateRec={
            id: this.record.id,
            type:this.record.type,
            className:this.record.className,
            version: this.record.version,
            published: this.record.published,
            features:this.record.features,
            values:this.record.values,
            hier:this.record.hier,
            web:this.record.web,
            others:this.record.others,
            // createTime: dayjs.localeData(),
            // updateTime: (new Date()).toLocaleDateString(),
            comments: this.record.comments
        }*/
        let result = await this.api.update(data).then(flag=>{
            if(flag){
                return true;
            }else{
                return false;
            }
        }).catch(err=>{
            return false;
        });
        return result;
    }

    /**
     * 查询
     * @param param
     * @returns {Promise<*>}
     */
    async query(filter){
        let result = await this.api.query({condition:filter}).then(data=>{
            if(!!data){
                this.setRecord(data[0]);
                return true;
            }else{
                return false;
            }
        }).catch(err=>{
            return false;
        });
        return result;
    }

    /**
     * 新增模板
     * @returns {{features: *[], comments: string, web: {}, values: {}, className: string, published: string, type: string, version: string, hier: *[], others: {}}}
     */
    static tpl(){
        return {
            // id: "",
            type:"",
            className:"",
            version: "1.0",
            published: true,
            features:[],
            values:{
                "general": [
                    {propertyName:"name", propertyType:"String",defaultValue:"",filter:"false", comments:""},
                ]
            },
            hier:[],
            web:{},
            others:{},
            // createTime: dayjs.localeData(),
            // updateTime: (new Date()).toLocaleDateString(),
            comments: ""
        }
    }

    /*******************************************************************************
     * 版本管理
     ******************************************************************************/
    /**
     * 版本升级
     * @returns {Promise<*>}
     */
    async upgrade(className,version){
        let result = await this.api.upgrade(className,version).then(flag=>{
            if(flag){
                return true;
            }else{
                return false;
            }
        }).catch(err=>{
            return false;
        });
        return result;
    }


    /**
     * 发布
     * @returns {Promise<*>}
     */
    async published(className,version){
        let result = await this.api.published(className,version).then(flag=>{
            if(flag){
                return true;
            }else{
                return false;
            }
        }).catch(err=>{
            return false;
        });
        return result;
    }

    /**
     * 取消发布
     * @returns {Promise<*>}
     */
    async unpublished(className,version){
        let result = await this.api.unpublished(className,version).then(flag=>{
            if(flag){
                return true;
            }else{
                return false;
            }
        }).catch(err=>{
            return false;
        });
        return result;
    }
}
